package com.tanhua.manage.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.manage.mapper.OperationLogMapper;
import com.tanhua.manage.pojo.OperationLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class OperationLogService extends ServiceImpl<OperationLogMapper, OperationLog>{

    /**
     *
     * @param id 日志id
     * @return 日志信息
     */
    public OperationLog queryById(Long id) {
        return super.getOne(Wrappers.<OperationLog>lambdaQuery().eq(OperationLog::getId,id));
    }

    /**
     * 获取发布内容分页数据
     *
     * @param page      当前页码
     * @param pageSize  页尺寸
     * @param username  用户名
     * @param sd        开始时间
     * @param ed        结束时间
     * @param operation        结束时间
     * @param describe        结束时间
     * @return 发布内容分页数据
     */
    public IPage<OperationLog> findInfoByPage(Integer page, Integer pageSize, String username, Long sd, Long ed, String operation, String describe) {


        //分页排序
        Page<OperationLog> infoPage = new Page<>(page, pageSize);
        infoPage.setDesc("log_time");

        LambdaQueryWrapper<OperationLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(username)) {
            lambdaQueryWrapper.like(OperationLog:: getUsername,username);
        }
        if (StrUtil.isNotEmpty(operation)) {
            lambdaQueryWrapper.like(OperationLog::getOperation, operation);
        }
        if (StrUtil.isNotEmpty(describe)) {
            lambdaQueryWrapper.like(OperationLog::getDesCribe, describe);
        }

        if (sd != null && ed != null && sd != -1 && sd.equals(ed)) {
            // 当 开始时间 和 结束时间 为相同时，即同一天。结束时间+1天毫秒数
            ed = ed + 86400000;
        }
        if (sd != null && sd > 0) {
            lambdaQueryWrapper.ge(OperationLog::getLogTime, sd);
        }
        if (ed != null && ed > 0) {
            lambdaQueryWrapper.le(OperationLog::getLogTime, ed);
        }

        return super.getBaseMapper().selectPage(infoPage, lambdaQueryWrapper);
    }

    /**
     *
     * @param operationLog 日志
     * @return
     */
    public boolean saveOperationLog(OperationLog operationLog){
        return super.save(operationLog);
    }


}
